package com.vue.admin.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.db.TableService;
import com.vue.admin.system.model.SysDept;
import com.vue.admin.system.service.DeptService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname DeptServiceImpl
 * @Description TODO
 * @Date 2022/8/21 11:46
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    TableService tableService;

    @Override
    public SysDept getById(Object id) {
        SysDept sysDept=tableService.getOneById(id,SysDept.class);
        return sysDept;
    }

    @Override
    public SysDept updateById(SysDept sysDept) {
        tableService.updateById(sysDept,SysDept.class);
        return sysDept;
    }

    @Override
    public SysDept save(SysDept sysDept) {
        tableService.save(sysDept,SysDept.class);
        return sysDept;
    }

    @Override
    public List<SysDept> list() {
        List<SysDept> sysDeptList=tableService.getList(SysDept.class);
        return sysDeptList;
    }

    @Override
    public List<SysDept> excludeList(Object id) {
        List<SysDept> sysDeptList=this.list();
        List<SysDept> sysList=new ArrayList<SysDept>();
        sysDeptList.stream().filter(e->{
            return String.valueOf(e.getDeptId()).equals(String.valueOf(id));
        }).map(e->{
            dealDeptExcludeChildren(e.getDeptId(),sysDeptList,sysList);
            sysList.add(e);
            return e;
        }).collect(Collectors.toList());

        List<SysDept> sysDeptListNew = new ArrayList<SysDept>();
        for (int i = 0; i < sysDeptList.size() ; i++) {
            SysDept sysDept=sysDeptList.get(i);
            System.out.println(JSONObject.toJSONString(sysDept));
            if(!sysList.contains(sysDept)){
                sysDeptListNew.add(sysDept);
            }
        }
        return sysDeptListNew;
    }

    @Override
    public JSONArray deptTreeList() {
        List<SysDept> sysDeptList=this.list();
        JSONArray arr=new JSONArray();
        sysDeptList.stream().filter(e->{
            return String.valueOf(e.getParentId()).equals("0");
        }).map(e->{
            JSONObject obj=dealDeptToJSONObject(e);
            obj.put("children",dealDeptTreeChildren(e.getDeptId(),sysDeptList));
            arr.add(obj);
            return e;
        }).collect(Collectors.toList());
        return arr;
    }

    public JSONArray dealDeptTreeChildren(Object id,List<SysDept> all){
        JSONArray arr=new JSONArray();
        all.stream().filter(e->{
            return String.valueOf(e.getParentId()).equals(String.valueOf(id));
        }).map(e->{
            JSONObject obj=dealDeptToJSONObject(e);
            obj.put("children",dealDeptTreeChildren(e.getDeptId(),all));
            arr.add(obj);
            return e;
        }).collect(Collectors.toList());
        return arr;
    }

    public JSONObject dealDeptToJSONObject(SysDept sysDept){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id",sysDept.getDeptId());
        jsonObject.put("label",sysDept.getDeptName());
        return jsonObject;
    }

    public void dealDeptExcludeChildren(Object id, List<SysDept> all, List<SysDept> add){
        all.stream().filter(e->{
            return String.valueOf(e.getParentId()).equals(String.valueOf(id));
        }).map(e->{
            dealDeptExcludeChildren(e.getDeptId(),all,add);
            add.add(e);
            return e;
        }).collect(Collectors.toList());
    }
}
